﻿<template>
  <el-dialog
    v-drag-dialog
    :title="dlgTitle"
    :visible.sync="dialogFormVisible"
    ref="dlg"
    width="800px"
    :close-on-click-modal="false"
  >
    <el-form
      :rules="rules"
      ref="dataForm"
      :model="temp"
      label-position="right"
      label-width="130px"
      size="small"
      style="width: 80%; margin-left:6px;"
    >
      <el-form-item label="学生序号：" prop="StudentDataID" v-if="temp.StudentDataID>0">
        <el-input placeholder="学生序号" v-model="temp.StudentDataID" :disabled="temp.StudentDataID>0"></el-input>
      </el-form-item>
      <el-form-item label="学生学号：" prop="StudentNo">
        <el-input placeholder="学生学号" v-model="temp.StudentNo" :disabled="temp.StudentDataID>0"></el-input>
      </el-form-item>
      <el-form-item label="学生姓名：" prop="StudentName">
        <el-input placeholder="学生姓名" v-model="temp.StudentName" :disabled="temp.StudentDataID>0"></el-input>
      </el-form-item>
      <el-form-item label="手机号码：" prop="StudentMobile">
        <el-input placeholder="手机号码" v-model="temp.StudentMobile" ></el-input>
      </el-form-item>
      <el-form-item label="学生状态：" prop="StudentState">
        <el-select class="filter-item" v-model="temp.StudentState" width="100%">
          <el-option
            v-for="item in studentStateOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学生年级：" prop="StudentGrade">
        <el-select class="filter-item" v-model="temp.StudentGrade" width="100%">
          <el-option
            v-for="item in studentGradeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学生专业：" prop="StudentMajor">
        <el-select class="filter-item" v-model="temp.StudentMajor" width="100%">
          <el-option
            v-for="item in studentMajorOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="性别：" prop="StudentSex">
        <el-select class="filter-item" v-model="temp.StudentSex" width="100%">
          <el-option
            v-for="item in studentSexOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="籍贯：" prop="StudentNative">
        <el-select class="filter-item" v-model="temp.StudentNative" width="100%">
          <el-option
            v-for="item in studentNativeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="上传图片：">
        <el-upload
          class="avatar-uploader"
          :action="uploadURL"
          :show-file-list="false"
          :on-success="handleUploadSuccess">
          <img v-if="temp.StudentImage" :src="imageURL" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <el-input disabled v-model="temp.StudentImage"></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="Cancel">取消</el-button>
      <el-button type="primary" @click="Save">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>

import { saveStudentInfo, getStudentInfo } from '@/api/student';
export default {
  components: {
    
  },
  data() {
    let checkMobile = (rule, value, callback) => {
      if (!/^1[3-9]\d{9}$/.test(value)) {
        callback(new Error('请输入正确的手机号'));
      } else callback();
    };
    return {
      dlgTitle: '',
      dialogFormVisible: false,
      baseURL: process.env.BASE_API,
      imageURL: '',
      uploadURL: process.env.BASE_API + '/Student/UploadFile',
      temp: {
        StudentDataID: 0,
        StudentImage:'',
      },
      rules: {
        StudentNo: [
          { required: true, message: '必填字段', trigger: 'blur' }
        ],
        StudentName: [
          { required: true, message: '必填字段', trigger: 'blur' }
        ],
        StudentState: [
          { required: true, message: '必填字段', trigger: 'blur' }
        ],
        StudentMobile: [
          { required: true, message: '必填字段', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ],
        StudentGrade: [
          { required: true, message: '必填字段', trigger: 'blur' }
        ],
        StudentMajor: [
          { required: true, message: '必填字段', trigger: 'blur' }
        ],
        StudentSex: [
          { required: true, message: '必填字段', trigger: 'blur' }
        ],
        StudentNative: [
          { required: true, message: '必填字段', trigger: 'blur' }
        ],
      },
      studentStateOptions: [
        {
          value: 1,
          label: "在读"
        },
        {
          value: 0,
          label: "删除"
        },
        {
          value: 2,
          label: "休学"
        },
        {
          value: 3,
          label: "退学"
        }
      ],
      studentSexOptions: [
        {
          value: "男",
          label: "男"
        },
        {
          value: "女",
          label: "女"
        }
      ],
      studentGradeOptions: [
        {
          value: 2018,
          label: "2018"
        },
        {
          value: 2019,
          label: "2019"
        },
        {
          value: 2020,
          label: "2020"
        },
        {
          value: 2021,
          label: "2021"
        }
      ],
      studentMajorOptions: [
        {
          value: "计算机科学与技术",
          label: "计算机科学与技术"
        },
        {
          value: "物联网工程",
          label: "物联网工程"
        },
        {
          value: "人工智能",
          label: "人工智能"
        },
        {
          value: "数学与应用数学",
          label: "数学与应用数学"
        }
      ],
      studentNativeOptions: [
        {
          value: "重庆",
          label: "重庆"
        },
        {
          value: "四川",
          label: "四川"
        },
        {
          value: "陕西",
          label: "陕西"
        },
        {
          value: "云南",
          label: "云南"
        },
        {
          value: "贵州",
          label: "贵州"
        },
        {
          value: "广西",
          label: "广西"
        }
      ],
    };
  },
  methods: {
    AddDialog(data) {
      let row = data || null;
      this.initInfo();
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate();
      });
      this.dlgTitle = '添加学生';
    },
    EditDialog(id) {
      this.initInfo();
      this.dialogFormVisible = true;
      this.temp.StudentDataID = id;
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate();
      });
      this.dlgTitle = '修改学生';
      this.getRecordInfo(this.temp.StudentDataID);
    },
    initInfo() {
      this.temp.StudentDataID = 0;
      this.temp = {};
      this.temp.StudentImage = '';
    },
    getRecordInfo(id) {
      getStudentInfo(id).then(rsp => {
        if (rsp.code === 0) {
          let record = rsp.data;
          this.temp = record;
          this.imageURL = this.baseURL + this.temp.StudentImage;
        } else {
          this.dialogFormVisible = false;
          this.$message.error(rsp.msg);
        }
      });
    },
    Save() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm('确认保存？', '提示', {
            type: 'warning',
            callback: action => {
              if (action == 'confirm') {
                saveStudentInfo(this.temp).then(rsp => {
                  if (rsp.code === 0) {
                    this.dialogFormVisible = false;
                    this.$message.success(this.$refs.dlg.title + '成功!');
                    this.$emit('onRefresh');
                  } else {
                    this.$message.error(rsp.msg);
                  }
                });
              }
            }
          });
        }
      });
    },
    Cancel() {
      this.dialogFormVisible = false;

    },
    handleUploadSuccess(res, file) {
      this.temp.StudentImage = res.data.url
      this.imageURL = this.baseURL + this.temp.StudentImage
    }
  }
};
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
